import Vue from 'vue'
import CryptoJS from 'crypto-js'

import md5 from 'js-md5'

import sha1 from 'js-sha1'

const key_base="contentWindowHig"; // 加密秘钥的基值

const token='API'


export default {//加密
  /*
  word:需要加密的字符
  keyStr:加密的密匙
  */
  encrypt (word) { // 加密

  let keyStr=key_base

  var key = CryptoJS.enc.Utf8.parse(keyStr)

  var srcs = CryptoJS.enc.Utf8.parse(word)

  var encrypted = CryptoJS.AES.encrypt(srcs, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7}) // 加密模式为ECB，补码方式为PKCS5Padding（也就是PKCS7）

  return encrypted.toString()

  },
  //解密
  decrypt(word){

    let keyStr=key_base

    var key  = CryptoJS.enc.Utf8.parse(keyStr);//Latin1 w8m31+Yy/Nw6thPsMpO5fg==
    var decrypt = CryptoJS.AES.decrypt(word, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
    return CryptoJS.enc.Utf8.stringify(decrypt).toString();
  },
  createNonceStr(length = 8) {
    //生成随机字符

          let chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

          let str = "";

          let len=chars.length

          for (let i = 0; i < length; i++) {

              str+=chars[Math.round((Math.random()*(len-1)))]

          }

          return "z"+str;

 },
 /**

* @param $timeStamp 时间戳

* @param $randomStr 随机字符串

* @return string 返回签名

*/
 arithmetic(timestamp,randomStr){
    let arr=[timestamp,randomStr,token]

    //拼接成字符串

    let str =arr.join('');

   //进行加密

   let signature = sha1(str);

   signature = md5(signature);

   //转换成大写

   signature = signature.toUpperCase();

   return signature;
 }

}
